#include "gameselect.h"
#include "game.h"
#include "gameroom.h"
#include <QPainter>
#include <QIcon>
#include <QFont>
#include <QPushButton>
#include <QSound>
#include <QFile>
#include <QTextStream>
#include <QTextEdit>
#include <QDateTime>

GameSelect::GameSelect(QWidget *parent) : QWidget(parent)
{
    // 设置游戏关卡界面
    this->setFixedSize(1000, 800);
    this->setWindowTitle("关卡选择");
    this->setWindowIcon(QIcon(":/D:/snake/8.png"));

    // 设置字体格式
    QFont font("华文行楷", 20, QFont::ExtraLight, false);

    // 创建返回按钮
    QPushButton* backBtn = new QPushButton(this);
    QPixmap pix(":/D:/snake/9.png");
    backBtn->setIcon(pix);
    backBtn->move(this->width() * 0.9, this->height() * 0.9);

    connect(backBtn, &QPushButton::clicked, [=](){
    emit this->backGameHall();// emit 关键字触发信号
    QSound::play(":/D:/snake/9.png");
    });

     //返回游戏大厅界面
    connect(this, &GameSelect::backGameHall, [=](){
        this->hide();
        Game* gameHall = new Game;
        gameHall->show();
    });

    GameRoom* gameRoom = new GameRoom();


    // 设置简单模式按钮
    QPushButton* simpleBtn = new QPushButton(this);
    simpleBtn->setStyleSheet("QPushButton { background-color:#0072C6; color:white; "
                             "border:2px groove gray; border-radius:10px; padding:6px; }");
    simpleBtn->setFont(font);
    simpleBtn->setText("简单模式");
    simpleBtn->move(395, 140);

    connect(simpleBtn, &QPushButton::clicked, [=](){
        gameRoom->setTimeout(300);
        gameRoom->setGeometry(this->geometry());// 设置窗口固定
        this->hide();
        gameRoom->show();
        QSound::play(":/D:/photo/6.png");
    });


    // 设置正常按钮模式
    QPushButton* normalBtn = new QPushButton(this);
    normalBtn->setStyleSheet("QPushButton { background-color:#0072C6; color:white; "
                             "border:2px groove gray; border-radius:10px; padding:6px; }");
    normalBtn->setFont(font);
    normalBtn->setText("正常模式");
    normalBtn->move(395, 260);

    connect(normalBtn, &QPushButton::clicked, [=](){
        gameRoom->setTimeout(200);
        gameRoom->setGeometry(this->geometry());// 设置窗口固定
        this->hide();
        gameRoom->show();
        QSound::play(":/D:/photo/6.png");
    });


    // 设置困难模式
    QPushButton* hardBtn = new QPushButton(this);
    hardBtn->setStyleSheet("QPushButton { background-color:#0072C6; color:white; "
                             "border:2px groove gray; border-radius:10px; padding:6px; }");
    hardBtn->setFont(font);
    hardBtn->setText("困难模式");
    hardBtn->move(395, 380);

    connect(hardBtn, &QPushButton::clicked, [=](){
        gameRoom->setTimeout(100);
        gameRoom->setGeometry(this->geometry());// 设置窗口固定
        this->hide();
        gameRoom->show();
        QSound::play(":/D:/photo/6.png");
    });


    // 获取历史战绩
    QPushButton* hisBtn = new QPushButton(this);
    hisBtn->setStyleSheet("QPushButton { background-color:#0072C6; color:white; "
                             "border:2px groove gray; border-radius:10px; padding:6px; }");
    hisBtn->setFont(font);
    hisBtn->setWindowIcon(QIcon(":/D:/snake/8.png"));
    hisBtn->setText("历史战绩");
    hisBtn->move(395, 500);

    connect(hisBtn, &QPushButton::clicked, [=](){
        QSound::play(":/D:/snake/8.png");

        QFile file("D:/code/qt/Snake/1.txt");
        file.open(QIODevice::ReadOnly);

        QTextStream in(&file);// 创建文本流对象
        int data = in.readLine().toInt();// 读取第一行作为整形数据

        QWidget* newWidget = new QWidget;// 一定不能添加到对象书上
        newWidget->setWindowTitle("历史战绩");
        newWidget->setWindowIcon(QIcon(":/D:/snake/8.png"));
        newWidget->setFixedSize(500, 300);

        QTextEdit* edit = new QTextEdit(newWidget);// 将编辑框置于新窗口上
        edit->setFont(font);
        edit->setFixedSize(500, 300);


        // 获取系统时间
        QDateTime currentTime = QDateTime::currentDateTime();
        QString time = currentTime.toString("yyyy-MM-dd hh:mm:ss");

        edit->setText("时间：");
        edit->append(time);

        edit->setText("得分为：");
        edit->append(QString::number(data));

        newWidget->show();
    });

}


//重写绘图事件，绘制游戏关卡选择界面
void GameSelect::paintEvent(QPaintEvent *event)
{
    (void)event;

    QPainter painter(this);// 实例化画家对象

    QPixmap pix(":/D:/snake/8.png");// 实例化绘画设备

    painter.drawPixmap(0, 0, this->width(), this->height(), pix);// 绘画
}
